package JavaSE.Class5;

import java.util.Arrays;

public class Class5 {

    public static void Bob(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flg = true;
                }
            }
            if(flg==false){
                return;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr={1,3,2,4,5};
        Bob(arr);
        System.out.println(Arrays.toString(arr));
    }


    public static int EF(int[] arr,int n) {
        int right=arr.length-1;
        int left=0;
        int mid=(left+right)/2;
        while(left<=right) {
            mid=(left+right)/2;
            if(n>arr[mid]){
                left=mid+1;
            }
            else if(n<arr[mid]){
                right=mid-1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
    public static void main3() {
        int[] arr={2,4,18,15,5,7,8,9};
        Arrays.sort(arr);//排序
        System.out.println(Arrays.toString(arr));
        int ret1=Arrays.binarySearch(arr,9);//自带的二分查找
        int ret2=EF(arr,9);
        System.out.println(ret1+" "+ret2);
    }

    public static String myToString(int[] arr) {
        String ret="[";
        for (int i = 0; i < arr.length-1; i++) {
            ret+=(arr[i] + ", ");
        }
        ret+=(arr[arr.length-1]+"]");
        return ret;
    }

    public static void main2() {
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        String ret1=myToString(arr);
        String ret2=Arrays.toString(arr);
        System.out.println(ret1);
        System.out.println(ret2);
    }

    public static void main1() {
        int[] arr = {1,2,3,4,5,6,7,8};

        String ret= Arrays.toString(arr);
        System.out.println(ret);

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        //for-each
        for(int x:arr){
            System.out.print(x+" ");
        }
    }
}
